package com.taiyi.renaissancemall.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ActivityUtils;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.AppManager;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.LoginResultBean;
import com.taiyi.renaissancemall.ui.MainActivity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginView, LoginPresent> implements LoginView {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.weChatLoginIv)
    ImageView weChatLoginIv;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.verificationCodeEt)
    EditText verificationCodeEt;
    @BindView(R.id.getVerificationCodeTv)
    TextView getVerificationCodeTv;
    @BindView(R.id.top)
    ConstraintLayout base_top_layout;
    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.logoIv)
    ImageView logoIv;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.v1)
    View v1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.v2)
    View v2;
    @BindView(R.id.toRegisterTv)
    TextView toRegisterTv;
    @BindView(R.id.loginTv)
    TextView loginTv;
    private TimeCount timeCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresent initPresenter() {
        return new LoginPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.login));

        timeCount = new TimeCount(60000, 1000);

        String token = Utils.getSpUtils().getString("token");
//        if (!TextUtils.isEmpty(token)) {
//            startActivity(new Intent(this, MainActivity.class));
//            finish();
//        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(base_top_layout).init();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initEvent() {

    }

    class TimeCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getVerificationCodeTv.setEnabled(false);
            long time = millisUntilFinished % 1000 > 500 ? millisUntilFinished / 1000 + 1 : millisUntilFinished / 1000;
            getVerificationCodeTv.setText(String.valueOf(time + "秒"));
        }

        @Override
        public void onFinish() {
            getVerificationCodeTv.setText("重新获取");
            getVerificationCodeTv.setEnabled(true);
        }
    }

    @OnClick({R.id.backIv, R.id.getVerificationCodeTv, R.id.toRegisterTv, R.id.loginTv, R.id.weChatLoginIv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                AppManager.getAppManager().finishAllActivity();
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                finish();
                break;
            case R.id.getVerificationCodeTv:
                String phoneNumberR = getPhoneNumber();
                if (TextUtils.isEmpty(phoneNumberR)) {
                    ToastUtils.showShortToast("请输入手机号码");
                    return;
                }
                if (!RegexUtil.checkPhone(phoneNumberR)) {
                    ToastUtils.showShortToast("请填写正确手机号");
                    return;
                }
                showProgress();
                presenter.getVerificationCode();
                break;
            case R.id.toRegisterTv:
                ActivityUtils.next(this, RegisterActivity.class);
                break;
            case R.id.loginTv:
                String phoneNumber = getPhoneNumber();
                if (TextUtils.isEmpty(phoneNumber)) {
                    ToastUtils.showShortToast("请输入手机号码");
                    return;
                }
                if (!RegexUtil.checkPhone(phoneNumber)) {
                    ToastUtils.showShortToast("请填写正确手机号");
                    return;
                }
                String verificationCode = getVerificationCode();
                if (TextUtils.isEmpty(verificationCode)) {
                    ToastUtils.showShortToast("请输入验证码");
                    return;
                }

                showProgress();
                presenter.login();
                break;
            case R.id.weChatLoginIv:
                IWXAPI iwxapi = MyApp.getWxApi();
                if (!iwxapi.isWXAppInstalled()) {
                    ToastUtils.showShortToast("您的设备未安装微信客户端");
                } else {
                    final SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test";
                    iwxapi.sendReq(req);
                }
                break;
        }
    }

    @Override
    public String getVerificationCode() {
        return verificationCodeEt.getText().toString();
    }

    @Override
    public String getPhoneNumber() {
        return phoneNumberEt.getText().toString();
    }

    @Override
    public void loginSuccess(LoginResultBean loginResultBean) {
        hideProgress();
        Utils.getSpUtils().putString("token", loginResultBean.getToken());
        ActivityUtils.next(this, MainActivity.class, true);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AppManager.getAppManager().finishAllActivity();
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("type", 1);
        startActivity(intent);
        finish();
    }

    @Override
    public void loginFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void getVerificationSuccess() {
        hideProgress();
        timeCount.start();
    }

    @Override
    public void getVerificationFailure() {
        hideProgress();
    }
}
